package test;

import java.io.IOException;
import java.net.*;
import java.nio.channels.DatagramChannel;

public class Send {
    public static void main(String[] args) {
        Send send = new Send();
        try {
            send.fun();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void fun() throws IOException {
        //1、基于UDP协议创建Socket对象   无需指定端口号ip地址
        DatagramSocket ds = new DatagramSocket();
        //2、创建数据包对象
        byte[] buf = "你好接收端".getBytes();
        DatagramPacket dp = new DatagramPacket(buf,buf.length, InetAddress.getByName("localhost"),9999);
        //3、发送数据包
        ds.send(dp);
        //4、释放资源
        ds.close();
    }
}
